#-*- coding:utf-8 -*-
# doc2pdf.py: python script to convert doc to pdf with bookmarks!
# Requires Office 2007 SP2
# Requires python for win32 extension
'''
参考:http://blog.csdn.net/rumswell/article/details/7434302
'''
import sys, os
from win32com.client import Dispatch, constants, gencache
# from config import REPORT_DOC_PATH,REPORT_PDF_PATH

def word2pdf(dir,filename):

  input=filename+'.docx'
  output=filename+'.pdf'
  pdf_name=output

  #判断文件是否存在
  os.chdir(dir)
  if not os.path.isfile(input):
    print u'%s not exist'%input
    return False
  #文档路径需要为绝对路径，因为Word启动后当前路径不是调用脚本时的当前路径。
  if (not os.path.isabs(input)):#判断是否为绝对路径
    #os.chdir(REPORT_DOC_PATH)
    input = os.path.abspath(input)#返回绝对路径
  else:
    print u'%s not absolute path'%input
    return False
  if (not os.path.isabs(output)):
    os.chdir(dir)
    output = os.path.abspath(output)
  else:
    print u'%s not absolute path'%output
    return False

  try:
    print input,output
    # enable python COM support for Word 2007
    # this is generated by: makepy.py -i "Microsoft Word 12.0 Object Library"
    gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4)
    #开始转换
    w = Dispatch("Word.Application")
    try:
      doc = w.Documents.Open(input, ReadOnly = 1)
      doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF,\
        Item = constants.wdExportDocumentWithMarkup, CreateBookmarks = constants.wdExportCreateHeadingBookmarks)
    except:
      print ' exception'
    finally:
      w.Quit(constants.wdDoNotSaveChanges)

    if os.path.isfile(pdf_name):
      print 'translate success'
      return True
    else:
      print 'translate fail'
      return False
  except:
    print ' exception'
    return -1
def word2pdf(dir,filename):

  input=filename+'.doc'
  output=filename+'.pdf'
  pdf_name=output

  #判断文件是否存在
  os.chdir(dir)
  if not os.path.isfile(input):
    print u'%s not exist'%input
    return False
  #文档路径需要为绝对路径，因为Word启动后当前路径不是调用脚本时的当前路径。
  if (not os.path.isabs(input)):#判断是否为绝对路径
    #os.chdir(REPORT_DOC_PATH)
    input = os.path.abspath(input)#返回绝对路径
  else:
    print u'%s not absolute path'%input
    return False
  if (not os.path.isabs(output)):
    os.chdir(dir)
    output = os.path.abspath(output)
  else:
    print u'%s not absolute path'%output
    return False

  try:
    print input,output
    # enable python COM support for Word 2007
    # this is generated by: makepy.py -i "Microsoft Word 12.0 Object Library"
    gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4)
    #开始转换
    w = Dispatch("Word.Application")
    try:
      doc = w.Documents.Open(input, ReadOnly = 1)
      doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF,\
        Item = constants.wdExportDocumentWithMarkup, CreateBookmarks = constants.wdExportCreateHeadingBookmarks)
    except:
      print ' exception'
    finally:
      w.Quit(constants.wdDoNotSaveChanges)

    if os.path.isfile(pdf_name):
      print 'translate success'
      return True
    else:
      print 'translate fail'
      return False
  except:
    print ' exception'
    return -1
if __name__=='__main__':

    path = r"D:\质检".decode("utf-8")

    pic = os.listdir(path)
    os.chdir(path)

    for dir, a, file in os.walk(path):
        for eachpic in file:
            filename = dir+os.sep+eachpic
            print(filename)
            if filename.endswith(".doc"):

                rc = word2pdf(dir,eachpic[:-4])
            if filename.endswith(".docx"):

                rc = wordx2pdf(dir,eachpic[:-5])

            else:
                continue